package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Awards.Applications;
import com.ruoyi.system.domain.Awards.TeacherReviews;
import com.ruoyi.system.mapper.ApplicationsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TeacherReviewsMapper;
import com.ruoyi.system.service.ITeacherReviewsService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-17
 */
@Service
public class TeacherReviewsServiceImpl implements ITeacherReviewsService
{
    @Autowired
    private TeacherReviewsMapper teacherReviewsMapper;
    @Autowired
    private ApplicationsMapper applicationsMapper;
    /**
     * 查询【请填写功能名称】
     *
     * @param reviewId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public TeacherReviews selectTeacherReviewsByReviewId(Long reviewId)
    {
        return teacherReviewsMapper.selectTeacherReviewsByReviewId(reviewId);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param teacherReviews 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TeacherReviews> selectTeacherReviewsList(TeacherReviews teacherReviews)
    {
        return teacherReviewsMapper.selectTeacherReviewsList(teacherReviews);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param teacherReviews 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTeacherReviews(TeacherReviews teacherReviews)
    {
        teacherReviews.setCreateTime(DateUtils.getNowDate());
        return teacherReviewsMapper.insertTeacherReviews(teacherReviews);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param teacherReviews 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTeacherReviews(TeacherReviews teacherReviews)
    {
        teacherReviews.setUpdateTime(DateUtils.getNowDate());
        return teacherReviewsMapper.updateTeacherReviews(teacherReviews);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param reviewIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTeacherReviewsByReviewIds(Long[] reviewIds)
    {
        return teacherReviewsMapper.deleteTeacherReviewsByReviewIds(reviewIds);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param reviewId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTeacherReviewsByReviewId(Long reviewId)
    {
        return teacherReviewsMapper.deleteTeacherReviewsByReviewId(reviewId);
    }

    @Override
    public List<TeacherReviews> getReviews(TeacherReviews teacherReviews) {
        return  teacherReviewsMapper.getReviews(teacherReviews);
    }

    @Override
    public AjaxResult doReview(TeacherReviews teacherReviews) {
        if(teacherReviews.getReviewResult()==null||teacherReviews.getReviewResult().equals("")){
            return AjaxResult.error("审核结果不能为空");
        }
        TeacherReviews teacher=new TeacherReviews();
        teacher.setReviewId(teacherReviews.getReviewId());
        teacher.setReviewResult(teacherReviews.getReviewResult());
        teacher.setReviewComment(teacherReviews.getReviewComment());
        teacher.setRecommendedReason(teacherReviews.getRecommendedReason());
        teacher.setReviewDate(DateUtils.getNowDate());
        teacherReviewsMapper.updateTeacherReviews(teacher);
        if("通过".equals(teacherReviews.getReviewResult())){
            //学校审核人通过
            if(Integer.valueOf(teacherReviews.getReviewerLevel())==3){
                Applications applications =new Applications();
                applications.setApplicationId(teacherReviews.getApplicationId());
                applications.setApplicationStatus("审核通过");
                applicationsMapper.updateApplications(applications);
                return AjaxResult.success();
            }
            //通知下一个人审核
            TeacherReviews nextTeacherReviews=new TeacherReviews();
            nextTeacherReviews.setApplicationId(teacherReviews.getApplicationId());
            nextTeacherReviews.setReviewerId(teacherReviews.getReviewerId());
            nextTeacherReviews.setReviewer(teacherReviews.getReviewer());
            nextTeacherReviews.setReviewerLevel(teacherReviews.getReviewerLevel());
            nextTeacherReviews.setReviewResult("待审核");
            teacherReviewsMapper.insertTeacherReviews(nextTeacherReviews);
        }else {
            //通知学生重新提交
            Applications applications =new Applications();
            applications.setApplicationId(teacherReviews.getApplicationId());
            applications.setApplicationStatus("已退回");
            applicationsMapper.updateApplications(applications);
        }
        return AjaxResult.success();
    }
}
